//
//  ThirdScrollTableViewCell.m
//  大众团购
//
//  Created by 安东 on 14-10-15.
//  Copyright (c) 2014年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "ThirdScrollTableViewCell.h"

@implementation ThirdScrollTableViewCell

- (void)dealloc
{
    [_scroll release];
    [_page release];
    
    [super dealloc];
}
-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // Initialization code
        
        self.scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 120)];
        _scroll.contentSize = CGSizeMake(320 * 4, 120);
        _scroll.pagingEnabled = YES;
        _scroll.showsHorizontalScrollIndicator = NO;
        _scroll.delegate = self;
        _scroll.backgroundColor = [UIColor lightGrayColor];
        [self.contentView addSubview:_scroll];
        [_scroll release];
        
        
        self.page = [[UIPageControl alloc] initWithFrame:CGRectMake(0, 90, 320, 30)];
        _page.numberOfPages = 4;
        [_page addTarget:self action:@selector(pageControlAction) forControlEvents:UIControlEventValueChanged];
        [self.contentView addSubview:_page];
        [_page release];
        //_page.backgroundColor = [UIColor blackColor];
        [NSTimer scheduledTimerWithTimeInterval:3 target:self selector:@selector(timeAction) userInfo:nil repeats:YES];
        
        
    }
    return self;
}
- (void)timeAction
{
    if (_count == 3) {
        _count = -1;
    }
    _scroll.contentOffset = CGPointMake(320 + 320 * _count, 0);
    _count++;
    _page.currentPage = _scroll.contentOffset.x/320;
}
- (void)pageControlAction
{
    
    [_scroll setContentOffset:CGPointMake(_page.currentPage * 320, 0 ) animated:YES];
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    [_page setCurrentPage:scrollView.contentOffset.x / 320];
    //    if (scrollView.contentOffset.x == 0) {
    //        scrollView.contentOffset = CGPointMake(320 * 3, 0);
    //        _page.currentPage = 3;
    //    }
    //    if (scrollView.contentOffset.x == 320 * 5){
    //        scrollView.contentOffset = CGPointMake(320, 0);
    //        _page.currentPage = 0;
    //    }
    
}

- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
